package com.muyu.vehicle.process;

import com.muyu.vehicle.config.Config;
import com.muyu.vehicle.enums.VehicleStatusEnum;
import com.muyu.vehicle.service.IVehicleFaultLogService;
import com.muyu.vehicle.service.IVehicleInfoService;
import com.vehicle.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author 牧鱼
 * @Classname ProcessVehicleOnLineThread
 * @Description TODO
 * @Date 2021/9/15
 */
public class ParsingVehicleLogThread implements Runnable{

    private static final Logger log = LoggerFactory.getLogger(ParsingVehicleLogThread.class);

    private RedisTemplate<String,String> redisTemplate;
    private IVehicleFaultLogService vehicleInfoService;

    public ParsingVehicleLogThread(RedisTemplate<String, String> redisTemplate, IVehicleFaultLogService vehicleInfoService) {
        this.redisTemplate = redisTemplate;
        this.vehicleInfoService = vehicleInfoService;
    }

    @Override
    public void run() {
        ListOperations<String, String> operations = redisTemplate.opsForList();
        String value = operations.rightPop(Config.VEHICLE_FAULT_LOG);
        if (StringUtils.isNotEmpty(value)){
            String[] split = value.split(",");
            String vin = split[0];
            String label = split[1];
            log.info("接收到车辆异常信息 VIN:{} 信息：{}",vin,label);
            vehicleInfoService.insertVehicleFaultLog(vin,label);
        }
    }
}
